package com.wing.user.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonDealUtils;
import com.wing.common.utils.JsonResult;
import com.wing.enterprisecab.feign.EnterpriseCabHttpClient;
import com.wing.enterprisecab.product.model.response.EnterpriseVO;
import com.wing.platform.feign.PlatformClient;
import com.wing.user.dao.CompanyInfoDao;
import com.wing.user.model.entity.CompanyInfo;
import com.wing.user.model.request.CompanyInfoForm;
import com.wing.user.model.response.CompanyInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 企业基本信息-ServiceImpl
 *
 * @author
 */
@Service
@Slf4j
public class CompanyInfoServiceImpl extends ServiceImpl<CompanyInfoDao, CompanyInfo> implements CompanyInfoService {

    @Autowired
    private EnterpriseCabHttpClient enterpriseCabHttpClient;
    @Autowired
    private PlatformClient platformClient;
    @Override
    public int add(CompanyInfoForm companyInfoForm) {

        //校验

        CompanyInfo companyInfo =new CompanyInfo();
        CopyOptions copyOptions = CopyOptions.create()
        .setEditable(CompanyInfo.class)
        .setIgnoreError(true)
        .setIgnoreNullValue(true);
        BeanUtil.copyProperties(companyInfoForm, companyInfo, copyOptions);
        String mark = baseMapper.getMax();
        if(StringUtils.isNotBlank(mark)){
           /* int max = Integer.parseInt(mark.substring(1));
            companyInfo.setMark("E"+String.format("%04d", max+1));*/
            //取到默认企业
            QueryWrapper<CompanyInfo> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("is_default",1);
            CompanyInfo defaultCompany = baseMapper.selectOne(queryWrapper);
            Integer integer = baseMapper.selectCount(null);
            companyInfo.setMark(defaultCompany.getMark()+"-"+String.format("%03d", integer));
        }else{
            companyInfo.setMark("A0001");
        }
        return baseMapper.insert(companyInfo);
    }

    @Override
    public List<CompanyInfoVO> list4app(Map<String, Object> params) {
        return baseMapper.list4app(params);
    }

    @Override
    public Page<CompanyInfoVO> pageList4app(Page page, Map<String, Object> params) {
        List<CompanyInfoVO> list = baseMapper.list4app(page, params);
        if(list.size() ==0){
            EnterpriseVO enterprise = enterpriseCabHttpClient.get().getData();
            log.info("enterprise:{}",enterprise);
            log.info("enterprise:{}",JSONObject.toJSONString(enterprise));
            JsonResult companyInfoByCode = platformClient.getCompanyInfoByCode(enterprise.getCode());
            if (companyInfoByCode.getCode()==200) {
                Object data = companyInfoByCode.getData();
                if(((Map)data).size()>0){
                    String s = JSON.toJSONString(data);
                    JSONObject jsonObject = JsonDealUtils.getNoNullValue(s);
                    CompanyInfo companyInfo = JSON.parseObject(jsonObject.toJSONString(), CompanyInfo.class);
                    //设置为默认企业
                    companyInfo.setIsDefault(true);
                    if(null != jsonObject.get("companyCode")){
                        companyInfo.setMark(jsonObject.get("companyCode").toString());
                    }
                    else if(null != jsonObject.get("businessLicenseCode")){
                        companyInfo.setLicenseNo(jsonObject.get("businessLicenseCode").toString());
                    }
                    baseMapper.insert(companyInfo);
                }
            }
            list = baseMapper.list4app(page, params);
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public CompanyInfoVO get4appById(Long id) {
        return baseMapper.get4appById(id);
    }

    @Override
    public CompanyInfoVO getCompanyByName(String name) {
        return baseMapper.getCompanyByName(name);
    }
}
